package com.eryos.android.cigarettecounter.tool;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.eryos.android.cigarettecounter.R;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class GoogleAnalyticsProvider {

	private static final String GOOGLE_ANALYTICS_TRACKER_ID = "UA-8541799-7";
	
	public static final int SCOPE_VISITOR_LEVEL     = 1;           
	public static final int SCOPE_SESSION_LEVEL     = 2;           
	public static final int SCOPE_PAGE_LEVEL        = 3;  

	private static final String APP_VERSION_TAG 	= "App_version";
	private static final String ANDROID_VERSION_TAG = "Android_version";
	private static final String SDK_TAG 			= "Android_sdk";
	private static final String BRAND_TAG 			= "Brand";
	private static final String DEVICE_TAG 			= "Device";
	private static final String APP_BUILD_TAG 		= "Build";

	private static boolean trackerStarted = false;
	private static GoogleAnalyticsTracker tracker;
	private static Context context;

	/**
	 * Start a new tracker or retrieve the last one used
	 * @param _context : application context
	 */
	public static void startTracker(Context _context){
		if ( tracker == null ) {
			context = _context;
			tracker = GoogleAnalyticsTracker.getInstance();
			Log.d("CigaretteCounter", "GoogleAnalytics : Tracker initialized ...");
		}
		if ( ! isTrackerStarted() ){
			tracker.start(GOOGLE_ANALYTICS_TRACKER_ID, context);
			trackerStarted = true;
			Log.d("CigaretteCounter", "GoogleAnalytics : Tracker started ...");
		}
	}

	/**
	 * Stop tracker and dispatch data to Analytics Service
	 */
	public static void stopTracker(){
		if ( isTrackerValid() ) {
			tracker.dispatch();
			tracker.stop();
			trackerStarted = false;
			Log.d("CigaretteCounter", "GoogleAnalytics : Tracker stoped");
		}
	}

	
	public static void trackPage(String page){
		Log.d("CigaretteCounter", "GoogleAnalytics : trackPage : "+page);
		if ( isTrackerValid() )
			tracker.trackPageView(page);
	}
	
	public static void dispatch(){
		Log.d("CigaretteCounter", "GoogleAnalytics : dispatch");
		if ( isTrackerValid() )
			tracker.dispatch();
	}
	
	public static void trackCustomVars(){
		if ( isTrackerValid() ){

			// Retrieve Device and Software informations 
			//PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);                           
			String appVersion = context.getString(R.string.version);//packageInfo.versionName;
			String buildVersion = context.getString(R.string.build);//packageInfo.versionName;
			String androidVersion = Build.VERSION.RELEASE;
			String sdk = Build.VERSION.SDK;
			String brand = Build.MANUFACTURER; 
			String device = Build.MANUFACTURER+" : "+Build.MODEL; 

			/* 
			    setCustomVar(SCOPE_VISITOR_LEVEL, APP_VERSION_TAG, appVersion);
				setCustomVar(SCOPE_VISITOR_LEVEL, APP_BUILD_TAG, buildVersion);
				setCustomVar(SCOPE_VISITOR_LEVEL, ANDROID_VERSION_TAG, androidVersion);
				setCustomVar(SCOPE_VISITOR_LEVEL, SDK_TAG, sdk);
				setCustomVar(SCOPE_VISITOR_LEVEL, BRAND_TAG, brand);
				setCustomVar(SCOPE_VISITOR_LEVEL, DEVICE_TAG, device);
			*/

			trackEvent("Apps_Infos", APP_VERSION_TAG, appVersion);
			trackEvent("Apps_Infos", APP_BUILD_TAG, buildVersion);
			trackEvent("OS", ANDROID_VERSION_TAG, androidVersion);
			trackEvent("OS", SDK_TAG, sdk);
			trackEvent("Device", BRAND_TAG, brand);
			trackEvent("Device", DEVICE_TAG, device);

		}
	}

	/**
	 * Define Custom Var
	 * @param scope : default is 1
	 * @param tag : Var Name 
	 * @param value : Var Value
	 * @return true if recorded
	 */
	public static boolean setCustomVar(int scope, String tag, String value){
		if ( isTrackerValid() ){
			boolean res = tracker.setCustomVar(1, tag, value, 2);
			Log.d("CigaretteCounter", "GoogleAnalytics : Track : "+tag+" : "+res);
			return res;
		}else
			return false;
	}
	
	public static boolean isTrackerStarted() {
		return trackerStarted;
	}
	
	public static boolean isTrackerValid(){
		if ( tracker!= null && isTrackerStarted() )
			return true;
		else 
			return false;
	}
	
	public static boolean trackEvent(String category, String eventName, String eventValue){
		tracker.trackEvent(category, eventName, eventValue, 1);
		return true;
	}

	public static boolean trackEvent(String eventName, String eventValue){
		tracker.trackEvent("Click", eventName, eventValue, 1);
		return true;
	}
}
